package lee.study.utils;

import com.alibaba.fastjson.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author licc
 * @create 2017-11-23 16:52
 * @desc 日期工具类
 **/
public class CalendarUtils {
    private static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
    private static SimpleDateFormat format_ymd = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 昨天时间段
     * @return
     */
    public static JSONObject getYesterDay(){
        JSONObject resultJson = new JSONObject();
        int year = calendar.get(Calendar.YEAR);
        // 第一个月为0月,故加1
        int month = calendar.get(Calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH)-1;

        resultJson.put("fromDate",""+year+"-"+month+"-"+day);
        resultJson.put("toDate",""+year+"-"+month+"-"+day);
        return resultJson;
    }

    /**
     * 本周的时间段
     * @return
     */
    public static JSONObject getCurrentWeek(){
        JSONObject resultJson = new JSONObject();
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        Calendar yesCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        yesCal.add(Calendar.DATE,-1);

        resultJson.put("fromDate",format_ymd.format(calendar.getTime()));
        resultJson.put("toDate",format_ymd.format(yesCal.getTime()));
        return resultJson;
    }

    /**
     * 本周的时间段
     * @return
     */
    public static JSONObject getLastWeek(){
        JSONObject resultJson = new JSONObject();
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        Calendar yesCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        yesCal.add(Calendar.DATE,-1);


        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(new Date()));
        cal.add(Calendar.DATE, -7);

        resultJson.put("fromDate",format_ymd.format(cal.getTime()));
        resultJson.put("toDate",format_ymd.format(yesCal.getTime()));
        return resultJson;
    }


    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 本月的时间段
     * @return
     */
    public static JSONObject getCurrentMonth(){
        JSONObject resultJson = new JSONObject();
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

        Calendar yesCal = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        yesCal.add(Calendar.DATE,-1);

        resultJson.put("fromDate",format_ymd.format(calendar.getTime()));
        resultJson.put("toDate",format_ymd.format(yesCal.getTime()));

        return resultJson;
    }

    public static Boolean isMonday(){
        Calendar rightNow=Calendar.getInstance();
        int day=rightNow.get(Calendar.DAY_OF_WEEK);
        if ((day-1)==1){
            return true;
        }else {
            return false;
        }
    }

    public static Boolean isFirstDayOfMonth(){
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day==1){
            return true;
        }else {
            return false;
        }
    }
}
